package pers.bc.utils.sql;

public class SqlBuffer
{
    
    private StringBuffer buffer = new StringBuffer();
    
    public String toString() { return this.buffer.toString(); }
    
    public void append(String str) {  this.buffer.append(str);  }
    
    public void append(Object obj)  { this.buffer.append(obj.toString());  }
    
    public void startParentheses()  { this.buffer.append(" ( "); }

    public void reset() { this.buffer.setLength(0);   }
    
    public void endParentheses() { this.buffer.append(" ) "); }
    
    public void append(String name, String value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append("='");
        this.buffer.append(value);
        this.buffer.append("' ");
    }
    
    public void append(String name, int value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append("=");
        this.buffer.append(value);
        this.buffer.append(" ");
    }
    
    public void append(String name, Double value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append("=");
        this.buffer.append(value);
        this.buffer.append(" ");
    }
    
    public void append(String name, Integer value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append("=");
        this.buffer.append(value);
        this.buffer.append(" ");
    }
    
    public void append(String name, String operator, String value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(operator);
        this.buffer.append(" '");
        this.buffer.append(value);
        this.buffer.append("' ");
    }
    
    public void append(String name, String operator, int value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(operator);
        this.buffer.append(value);
        this.buffer.append(" ");
    }
    
    public void append(String name, String operator, Double value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(operator);
        this.buffer.append(value);
        this.buffer.append(" ");
    }
    
    public void append(String name, String operator, Integer value)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(operator);
        this.buffer.append(value);
        this.buffer.append(" ");
    }
    
    public void append(String name, String[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(" in ( ");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append("'");
            this.buffer.append(values[i]);
            this.buffer.append("'");
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void appendNot(String name, String[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(" not in (");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append("'");
            this.buffer.append(values[i]);
            this.buffer.append("'");
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void append(String name, int[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(" in (");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append(values[i]);
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void appendNot(String name, int[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(" not in (");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append(values[i]);
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void append(String name, Double[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append("in (");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append(values[i]);
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void appendNot(String name, Double[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(" not in (");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append(values[i]);
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void append(String name, Integer[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(" in (");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append(values[i]);
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void appendNot(String name, Integer[] values)
    {
        this.buffer.append(" ");
        this.buffer.append(name);
        this.buffer.append(" not in (");
        int length = values.length;
        
        for (int i = 0; i < length; ++i)
        {
            this.buffer.append(values[i]);
            this.buffer.append(",");
        }
        
        length = this.buffer.length();
        this.buffer.deleteCharAt(length - 1);
        this.buffer.append(") ");
    }
    
    public void appendRound(String expression, int precision)
    {
        this.buffer.append(" round ( ");
        this.buffer.append(expression);
        this.buffer.append(",");
        this.buffer.append(precision);
        this.buffer.append(" ) ");
    }
    
    public void appendRound(String expression, Integer precision)
    {
        this.buffer.append(" round ( ");
        this.buffer.append(expression);
        this.buffer.append(",");
        this.buffer.append(precision);
        this.buffer.append(" ) ");
    }
    
    public void appendCaseWhen(String condition, String trueExpression, String falseExpression)
    {
        this.buffer.append(" case when ");
        this.buffer.append(condition);
        this.buffer.append(" then ");
        this.buffer.append(trueExpression);
        this.buffer.append(" else ");
        this.buffer.append(falseExpression);
        this.buffer.append(" end ");
    }
    
    public void appendin(String string, String condition)
    {
        this.buffer.append(" ");
        this.buffer.append(string);
        this.buffer.append(" ");
        this.buffer.append("in");
        this.buffer.append(" (");
        this.buffer.append(condition);
        this.buffer.append(") ");
    }
}
